﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.NuGetManagement;
using DWF.Studio.Project;
using System;
using System.Windows;

namespace DWF.Studio.Custom
{
    /// <summary>
    /// Nuget包包装类
    /// </summary>
    public class NugetPackageWrap : DependencyObject
    {
        /// <summary>
        /// nuget包
        /// </summary>
        private NuGetPackage _nugetPackage = null;
        private NuGetPackageMetadata _nuGetPackageMetadata = null;
        public NugetPackageWrap(NuGetPackage nugetPackage)
        {
            SetNuGetPackage(nugetPackage);
        }

        public NugetPackageWrap(PackageInfo packageInfo)
        {
            SetValue(IdProperty, packageInfo.Id);
            SetValue(VersionProperty, packageInfo.Version);
            SetValue(IconUriProperty, packageInfo.IconUri);
            IsInstalled = packageInfo.IsInstalled;
            IsProjectDependency = true;
        }
        /// <summary>
        /// 本地包源无法读取Summary所以引用Description 在NuGetPackageMetadata模型里
        /// </summary>
        /// <param name="nuGetPackageMetadata"></param>
        public NugetPackageWrap(NuGetPackageMetadata nuGetPackageMetadata)
        {
            _nuGetPackageMetadata = nuGetPackageMetadata;

            SetValue(IdProperty, nuGetPackageMetadata.Id);
            SetValue(VersionProperty, nuGetPackageMetadata.Version);
            SetValue(IconUriProperty, nuGetPackageMetadata.IconUri);
            if (_nuGetPackageMetadata.IconUri == null)
            {
                SetValue(IconUriProperty, new Uri("../Resources/Packagegrey.png", UriKind.Relative));
            }
            else if (_nuGetPackageMetadata.IconUri.Equals("http://www.inossem.com/"))
            {
                SetValue(IconUriProperty, new Uri("../Resources/Package/ProcessGoActivity.png", UriKind.Relative));
            }
            else
            {
                SetValue(IconUriProperty, _nuGetPackageMetadata.IconUri);
            }
            SetValue(DescriptionProperty, _nuGetPackageMetadata.Description);
            SetValue(SummaryProperty, _nuGetPackageMetadata.Summary);
            SetValue(TagsProperty, _nuGetPackageMetadata.Tags);
            SetValue(AuthorsProperty, _nuGetPackageMetadata.Authors);
            if (UpgradeVersion == null || new Version(UpgradeVersion) <= new Version(_nuGetPackageMetadata.Version))
            {
                SetValue(IsUpgradeProperty, Visibility.Hidden);
            }
            IsProjectDependency = true;
            
        }
        /// <summary>
        /// 设置nuget包  本地包目录没有时不加载图片和描述
        /// </summary>
        /// <param name="nugetPackage"></param>
        public void SetNuGetPackage(NuGetPackage nugetPackage,bool isWrap=true)
        {
            _nugetPackage = nugetPackage;

            SetValue(IdProperty, _nugetPackage.Id);
            SetValue(VersionProperty, _nugetPackage.Version);
            SetValue(TitleProperty, _nugetPackage.Title);
            if (isWrap)
            {
                if (_nugetPackage.IconUri == null)
                {
                    SetValue(IconUriProperty, new Uri("../Resources/Packagegrey.png", UriKind.Relative));
                }
                else if (_nugetPackage.IconUri.Equals("http://www.inossem.com/"))
                {
                    SetValue(IconUriProperty, new Uri("../Resources/Package/ProcessGoActivity.png", UriKind.Relative));
                }
                else
                {
                    SetValue(IconUriProperty, _nugetPackage.IconUri);
                }
                if (_nugetPackage.Tags!=null)
                {
                    SetValue(TagsProperty, _nugetPackage.Tags);
                }
                
                SetValue(DescriptionProperty, _nugetPackage.Description);
                SetValue(SummaryProperty, _nugetPackage.Summary);
            }
            SetValue(AuthorsProperty, _nugetPackage.Authors);
            SetValue(PublishedOnProperty, _nugetPackage.PublishedOn);
            if (UpgradeVersion == null || new Version(UpgradeVersion) <= new Version(_nuGetPackageMetadata.Version))
            {
                SetValue(IsUpgradeProperty, Visibility.Hidden);
            }
        }

        /// <summary>
        /// NuGetPackage
        /// </summary>
        public NuGetPackage NuGetPackage
        {
            get
            {
                return _nugetPackage;
            }
        }

        /// <summary>
        /// 是否工程依赖包
        /// </summary>
        public bool IsProjectDependency
        {
            get;
            set;
        }

        /// <summary>
        /// 是否已经安装
        /// </summary>
        public bool IsInstalled
        {
            get;
            set;
        }

        /// <summary>
        /// 选择操作的
        /// </summary>
        public PackageOperationType PackageOperationType
        {
            get;
            set;
        }

        /// <summary>
        /// 更新前的版本
        /// </summary>
        public string ChoicePackageVersion
        {
            get;
            set;
        }

        /// <summary>
        /// nuget包Id
        /// </summary>
        public string Id
        {
            get
            {
                return (string)GetValue(IdProperty);
            }
        }

        /// <summary>
        /// nuget包版本
        /// </summary>
        public string Version
        {
            get
            {
                return (string)GetValue(VersionProperty);
            }
        }

        /// <summary>
        /// nuget包抬头
        /// </summary>
        public string Title
        {
            get
            {
                return (string)GetValue(TitleProperty);
            }
        }

        /// <summary>
        /// nuget包图片uri
        /// </summary>
        public Uri IconUri
        {
            get
            {
                return (Uri)GetValue(IconUriProperty);
            }
        }

        /// <summary>
        /// nuget包是否安装图片uri
        /// </summary>
        public Visibility StatusIcon
        {
            get;
            set;
        } = Visibility.Hidden;

        /// <summary>
        /// nuget按钮取消图片uri
        /// </summary>
        public Visibility ButtonIcon
        {
            get;
            set;
        } = Visibility.Hidden;

        /// <summary>
        /// nuget按钮更新图片uri
        /// </summary>
        public Visibility IsUpdate
        {
            get;
            set;
        } = Visibility.Hidden;

        /// <summary>
        /// nuget按钮下载图片uri
        /// </summary>
        public Visibility IsInstall
        {
            get;
            set;
        } = Visibility.Hidden;
        /// <summary>
        /// nuget按钮删除图片uri
        /// </summary>
        public Visibility IsDelete
        {
            get;
            set;
        } = Visibility.Hidden;
        /// <summary>
        /// 是否存在最新版本
        /// </summary>
        public Visibility IsUpgrade
        {
            get
            {
                return (Visibility)GetValue(IsUpgradeProperty);
            }
            set
            {
                SetValue(IsUpgradeProperty, value);
            }
        }
        /// <summary>
        /// 最新版本
        /// </summary>
        public string UpgradeVersion
        {
            get
            {
                return (string)GetValue(UpgradeVersionProperty);
            }
            set 
            { 
                SetValue(UpgradeVersionProperty, value);
            }
        }
        /// <summary>
        /// nuget包描述
        /// </summary>
        public string Description
        {
            get
            {
                return (string)GetValue(DescriptionProperty);
            }
        }

        /// <summary>
        /// nuget包摘要
        /// </summary>
        public string Summary
        {
            get
            {
                return (string)GetValue(SummaryProperty);
            }
        }
        /// <summary>
        /// 标签
        /// </summary>
        public string Tags
        {
            get
            {
                return (string)GetValue(TagsProperty);
            }
        }
        /// <summary>
        /// nuget包作者
        /// </summary>
        public string Authors
        {
            get
            {
                return (string)GetValue(AuthorsProperty);
            }
        }

        /// <summary>
        /// nuget包发布时间
        /// </summary>
        public DateTimeOffset? PublishedOn
        {
            get
            {
                return (DateTimeOffset?)GetValue(PublishedOnProperty);
            }
        }

        public static readonly DependencyProperty IdProperty = DependencyProperty.Register("Id", typeof(string), typeof(NugetPackageWrap));
        public static readonly DependencyProperty VersionProperty = DependencyProperty.Register("Version", typeof(string), typeof(NugetPackageWrap));
        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(NugetPackageWrap));
        public static readonly DependencyProperty IconUriProperty = DependencyProperty.Register("IconUri", typeof(Uri), typeof(NugetPackageWrap));
        public static readonly DependencyProperty DescriptionProperty = DependencyProperty.Register("Description", typeof(string), typeof(NugetPackageWrap));
        public static readonly DependencyProperty SummaryProperty = DependencyProperty.Register("Summary", typeof(string), typeof(NugetPackageWrap));
        public static readonly DependencyProperty AuthorsProperty = DependencyProperty.Register("Authors", typeof(string), typeof(NugetPackageWrap));
        public static readonly DependencyProperty PublishedOnProperty = DependencyProperty.Register("PublishedOn", typeof(DateTimeOffset?), typeof(NugetPackageWrap));
        public static readonly DependencyProperty TagsProperty = DependencyProperty.Register("Tags", typeof(string), typeof(NugetPackageWrap));
        public static readonly DependencyProperty IsUpgradeProperty = DependencyProperty.Register("IsUpgrade", typeof(Visibility), typeof(NugetPackageWrap));
        public static readonly DependencyProperty UpgradeVersionProperty = DependencyProperty.Register("UpgradeVersion", typeof(string), typeof(NugetPackageWrap));

    }
}
